package com.smt.take.presenter

import android.util.Log
import com.smt.take.model.beans.GoodsBean
import com.smt.take.model.beans.GoodsDetailsBean
import com.smt.take.model.beans.GoodsListBean
import com.smt.take.ui.activity.BusinessActivity
import com.smt.take.ui.fragment.GoodsFragment
import com.smt.take.utils.TakeOutApp

class GoodsPresenter(private val goodsFragment: GoodsFragment) :
    BasePresenter<GoodsBean>(goodsFragment.context) {

    val goodsList: MutableList<GoodsDetailsBean> = mutableListOf()
    var goodsTypeList: MutableList<GoodsListBean> = mutableListOf()

    fun getGoodsInfo(sellerId: String) {
        val goodsInfo = takeOutService.getGoodsInfo(sellerId)
        goodsInfo.enqueue(callback)
    }

    override fun setDataToUI(data: GoodsBean?) {
        Log.e("goods", "该商家一个有 " + data?.list?.size + " 个商品")
        // 是否有点餐记录
        val hasSelectInfo = (goodsFragment.activity as BusinessActivity).hasSelectInfo
        data?.list?.let { goodsTypeList = it }
        data?.list?.size?.let {
            for (i in 0 until it) {
                val goodsTypeInfo = data.list[i]
                var aTypeCount = 0//红点个数
                if (hasSelectInfo) {
                    aTypeCount =
                        TakeOutApp.instance.queryCacheSelectedInfoByTypeId(goodsTypeInfo.id)
                    goodsTypeInfo.redDotCount = aTypeCount//一个类别的记录
                }
                for (j in 0 until goodsTypeInfo.list.size) {
                    val goodsDetailsBean = goodsTypeInfo.list[j]
                    if (aTypeCount > 0) {
                        val count =
                            TakeOutApp.instance.queryCacheSelectedInfoByGoodsId(goodsDetailsBean.id)
                        goodsDetailsBean.count = count//具体商品的记录个数
                    }
                    goodsDetailsBean.typeName = goodsTypeInfo.name
                    goodsDetailsBean.typeId = goodsTypeInfo.id
                    goodsList.add(goodsDetailsBean)
                }
            }
        }
        //更新购物车UI
        (goodsFragment.activity as BusinessActivity).updateCartUI()
        goodsFragment.onGoodsTypeSuccess(data, goodsList)
    }

    // 根据商品类别id找到此类别第一个商品的位置
    fun getGoodsPositionByTypeId(typeId: Int): Int {
        var position = -1 // -1表示未找到
        for (i in 0 until goodsList.size) {
            val goodsDetailsBean = goodsList[i]
            if (goodsDetailsBean.typeId == typeId) {
                position = i
                break
            }
        }
        return position
    }

    // 根据类别id找到其在左侧列表中的position
    fun getTypePositionByTypeId(newTypeId: Int): Int {
        var position = -1
        for (i in 0 until goodsTypeList.size) {
            val goodsListBean = goodsTypeList[i]
            if (goodsListBean.id == newTypeId) {
                position = i
            }
        }
        return position
    }

    // 获取购物车数据
    fun getCartList(): MutableList<GoodsDetailsBean> {
        val cartList = mutableListOf<GoodsDetailsBean>()
        // count >0 的为购物车商品
        for (i in 0 until goodsList.size) {
            val goodsDetailsBean = goodsList[i]
            if (goodsDetailsBean.count > 0) {
                cartList.add(goodsDetailsBean)
            }
        }
        return cartList
    }

    // 清除购物车商品
    fun clearCart() {
        val cartList = getCartList()
        for (i in 0 until cartList.size) {
            val goodsInfo = cartList[i]
            goodsInfo.count = 0
        }
    }

}